package com.boot.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.boot.beans.Question;
import com.boot.beans.SimpleUser;
import com.boot.beans.UserQuestion;
import com.boot.common.BaseService;
import com.boot.web.dao.SQLMapper;
import com.boot.web.dao.UserQuestionMapper;

@Service("serviceQuestion")
@Repository
@Transactional(rollbackFor = Exception.class)
public class QuestionService extends BaseService<Question> {

	@Autowired
	private SQLMapper			sqlMapper;
	@Autowired
	private UserQuestionMapper	userQuestionMapper;

	public void updateUserAnswer(Question q, boolean checkAns, SimpleUser user) {
		if (checkAns) {
			sqlMapper.updateZheng(q.getId());
		} else {
			sqlMapper.updateCuo(q.getId());
		}
		if (user != null) {
			UserQuestion uq = new UserQuestion();
			uq.setUserId(user.getId());
			uq.setQuestionId(q.getId());
			if (checkAns) {
				userQuestionMapper.delete(uq);
			} else {
				UserQuestion temp = userQuestionMapper.selectOne(uq);
				if (temp == null) {
					userQuestionMapper.insert(uq);
				}
			}
		}
	}

}
